/*
Copyright (C) 2014  Johan Degraeve

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/gpl.txt>.

*/

package helpdiabetesj2me;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author Johan
 */
public class HelpDiabetes extends MIDlet {

    /**
     * 0 for Dutch, ...
     */
    static public int languageIndex = 0;

    /**
     * making it public because foodfile.FinishFoodfileCreation needs to start the thread for this object.
     */
    static public GetItemFromUser getitemfromuser;
   
    /**
    * food rs is the recordstore containing the foodfile items
    * it's used by all classes'
    *name of the recordstore is 'foodfile'
    */
    static public RecordStore foodrs = null;
    
    /**
     * settingsrs will be the recordstore containing the user's personal settings'
    *it's used by all classes'
    */
    static public RecordStore settingsrs = null;
    
    /**
     *selitemsrs will be the recordstore containing the items that the user already selected dur
    *it's used by all classes'
    *the recordstore will be opened in class AddItem, as soon as the user adds an item
    *name of the recordstore will be 'selecteditems'
    */
    static public RecordStore selitemsrs = null;
    
    static public HelpDiabetes midlet = null;
    
    static public String appname = "HelpDiabetes";
    
    /*
    *instance of the class FoodFile
    */
    static public FoodFile foodfileinst = null;
    
    public HelpDiabetes() {
        initialize();
    }
    
    private void initialize() {
         midlet = this;       
    }
    
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }
    
    /**
     * This method should exit the application.
     * 
     */
    public void exitMIDlet() {
        HDProperties.close();
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }
    
    public void startApp() {
        Display display = Display.getDisplay(this);
        
        // first i create the canvas that will actually be my main canvas
        getitemfromuser = new GetItemFromUser(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,((Integer)HDProperties.get("LetterGrootte")).intValue()),this);        
        display.setCurrent(getitemfromuser);
        getitemfromuser.resetflipfloptimer(getitemfromuser);
       
        getitemfromuser.setscrsize();//this method will set parameters screensize and height - needs to be done know while getitemfromuser is current display

        //first check if this app has ever run on this phone, if not then InitialScreen needs to be displayed
        //this check can only happen by checking the presence of a foodrs
        try {
            HelpDiabetes.foodrs = RecordStore.openRecordStore("foodfile", false);
        }  catch (RecordStoreFullException ex) {
            ex.printStackTrace();
        } catch (RecordStoreNotFoundException ex) {
            foodrs = null;
            //there's no foodrecord so assumption is that this is the first time this user starts the app
            new InitialScreen(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,((Integer)HDProperties.get("LetterGrootte")).intValue()),this);
        } catch (Exception e) {
              (new ReportException()).Report("Unexpected error while opening foodfile recordstore in HelpDiabetes.startApp()",e,false);
        }

        if (foodrs != null) {
          try {
              foodrs.closeRecordStore();
          }
          catch (Exception e) {

          }
          foodrs = null;
          try {
              foodfileinst = new FoodFile();
          } catch (Exception e) {
          }
        }
    }
    
    public void pauseApp() {
       
       getDisplay().setCurrent(null);
       //for ensuring that there's no more selitemsrs recordstore open.
       if (selitemsrs != null) {
            try {
                selitemsrs.closeRecordStore();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            } finally {
                selitemsrs = null;
            }
       }
       if (getitemfromuser.deepsearch != null) {
           getitemfromuser.deepsearch = null;
       }
    }
    
    public void destroyApp(boolean unconditional) {
        closeRS();
    }

    /**
    * method to close the record stores, to be called by pauseapp and destroyapp
    */
    public void closeRS () {
        try {
            if (foodrs !=null) foodrs.closeRecordStore();
            foodrs = null;
        } catch (Exception ex) {
            ;
        }
        try {
            if (selitemsrs != null) selitemsrs.closeRecordStore();
            selitemsrs = null;
        } catch (Exception ex) {
            ;
        }
        try {
            if (settingsrs != null) settingsrs.closeRecordStore();
            settingsrs = null;
        } catch (Exception ex) {
            ;
        } 
        
    }
  }
